var title;
var productData;
var newsData;
var announcementData;
var api = "/api/search/list";
$(function () {
    $("#warp-item").hide();
})

$(".tab a").on('click', function () {
    console.log()
    $(this).addClass("lin").parent().siblings().children().removeClass("lin");

    if ($(this).text() === "产品") {
        productRenderTable(productData);
    } else if ($(this).text() === "新闻") {
        newsRenderTable(newsData)
    } else {
        announcementRenderTable(announcementData)
    }

})

//enter键盘事件
$('.search').keydown(function (e) {
    if (e.keyCode === 13) {
        title = $(".search").val();
        if (!vaildeParam(title)) {
            layer.msg("请输入查询内容");
            return;
        }
        var data = {
            "title": title,
        }
        postRenderTable(data)
    }
});

//点击搜索按钮
$('.searchBut').on('click', function (e) {
    title = $(".search").val();
    if (!vaildeParam(title)) {
        layer.msg("请输入查询内容");
        return;
    }
    var data = {
        "title": title,
    }
    postRenderTable(data)
})

//产品模板
function productRenderTable(data) {
    var html = "";
    $(".list").empty();
    if (data.length > 0) {
        $.each(data, function (index, item) {
            html = $(' <div class="explore-item">\n' +
                '                        <div class="row">\n' +
                '                            <div class="col-sm-4">\n' +
                '                                <a href="' + item.url + '" target="_blank">\n' +
                '                                    <img class="cover"\n' +
                '                                         src="' + item.pic + '">\n' +
                '                                </a>\n' +
                '                            </div>\n' +
                '                            <div class="col-sm-8">\n' +
                '                                <a href="' + item.url + '" target="_blank">\n' +
                '                                    <h3>' + item.title + '</h3>\n' +
                '                                </a>\n' +
                '                                <p class="desc">' + item.series_name + '</p>\n' +
                '                                <a href="' + item.url + '" target="_blank" class="new-info" style="cursor: pointer;">\n' +
                '                                    了解更多\n' +
                '                                </a>\n' +
                '                            </div>\n' +
                '                        </div>\n' +
                '                    </div>');
            $(".list").append(html);
        })
    } else {
        $(".list").empty();
        $(".list").append("<div class=\"no-data text-left tag-data\">未找到结果</div>")
    }
}

//新闻模板
function newsRenderTable(data) {
    var html = "";
    $(".list").empty();
    if (data.length > 0) {
        $.each(data, function (index, item) {
            var curTime = new Date(item.modify_date).format("yyyy-MM-dd");
            html = $('                    <div class="explore-item">\n' +
                '                        <div class="row">\n' +
                '                            <div class="col-sm-4">\n' +
                '                                <a href="/news/socialResponsibility?id=' + item.id + '" target="_self">\n' +
                '                                    <img class="cover"\n' +
                '                                         src="' + item.img + '">\n' +
                '                                </a>\n' +
                '                            </div>\n' +
                '                            <div class="col-sm-8">\n' +
                '                                <a href="">\n' +
                '                                    <h3>' + item.title + '</h3>\n' +
                '                                </a>\n' +
                '                                <p class="desc"></p>\n' +
                '                                <p class="new-info">\n' +
                '                                    <span class="date">' + curTime + '</span>\n' +
                '                                    <a class="btn-news-series font-gotham" href="/news/socialResponsibility?id=' + item.id + '" target="_self">新闻</a>\n' +
                '                                </p>\n' +
                '                            </div>\n' +
                '                        </div>\n' +
                '                    </div>');
            $(".list").append(html);
        })
    } else {
        $(".list").empty();
        $(".list").append("<div class=\"no-data text-left tag-data\">未找到结果</div>")
    }
}

//公告模板
function announcementRenderTable(data) {
    var html = "";
    $(".list").empty();
    if (data.length > 0) {
        $.each(data, function (index, item) {
            var curTime = new Date(item.modify_date).format("yyyy-MM-dd");
            html = $('                                        <div>\n' +
                '                                            <div class="row">\n' +
                '                                                <div class="col-sm-8">\n' +
                '                                                    <a href="/news/socialResponsibility?id=' + item.id + '" target="_self">\n' +
                '                                                        <h3>'+item.title+'</h3>\n' +
                '                                                    </a>\n' +
                '                                                    <p class="new-info">\n' +
                '                                                        <span class="date">' + curTime + '</span>\n' +
                '                                                        <a class="btn-news-series font-gotham" href="/news/socialResponsibility?id=' + item.id + '" target="_self">公告</a>\n' +
                '                                                    </p>\n' +
                '                                                </div>\n' +
                '                                            </div>\n' +
                '                                        </div>');
            $(".list").append(html);
        })
    } else {
        $(".list").empty();
        $(".list").append("<div class=\"no-data text-left tag-data\">未找到结果</div>")
    }
}

function postRenderTable(data) {
    postJSON(api, data, function (res) {
        var newsCount = res.newsCount;
        var productCount = res.productCount;
        var announcementCount = res.announcementCount;
        productData = res.productData;
        newsData = res.newsData;
        announcementData = res.announcementData;
        $("#warp-item").show();
        $(".tab a").eq(0).addClass("lin");
        $(".product-num").text("(" + productCount + ")");
        $(".news-num").text("(" + newsCount + ")");
        $(".announcement-num").text("(" + announcementCount + ")");
        productRenderTable(productData)
    })
}